#!/usr/bin/evn python
# -*- coding: UTF-8 -*-
# @File: base_page.py
# @Author: fy
# @date: 2021/9/6 12:04
import logging

from appium import webdriver
import unittest
from appium.webdriver.webdriver import WebDriver
from appium.webdriver.common.mobileby import MobileBy
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from time import sleep


class BasePage:

    def __init__(self, driver: WebDriver = None):
        self.driver = driver

    def back(self):
        self.driver.back()

    def find_element(self, locator, timeout=30):
        '''
        查找元素
        :param by:
        :return: web element
        '''
        print(locator)
        logging.info(locator)

        try:
            def isExist(driver):
                eles = self.driver.find_elements(*locator)
                if len(eles) >= 1:
                    return eles[0]
                else:
                    return None

            return WebDriverWait(self.driver, timeout).until(isExist)
        except TimeoutException:
            return None
        except Exception as e:
            print(e)
            return None

    def find_elements(self, locator, timeout=10):
        '''
        查找元素
        :param by:
        :return: web element
        '''
        print(locator)
        logging.info(locator)

        try:
            def isExist(driver):
                eles = self.driver.find_elements(*locator)
                if len(eles) >= 1:
                    return eles
                else:
                    return []
            return WebDriverWait(self.driver, timeout).until(isExist)
        except TimeoutException:
            return None
        except Exception as e:
            print(e)
            return None

    def find_and_click(self, locator, timeout=50):
        '''
        查找并点击
        :param by:
        :param value:
        :return:
        '''
        try:
            return self.find_element(locator).click()
        except TimeoutException:
            return None
        except Exception as e:
            print(e)
            return None

    def scroll_find_element(self, locator: tuple, times=1000):
        '''

        :param locator: 要查找的元素定位
        :param times: 滑动次数，默认直到找到为止
        :return: element
        '''
        window_size_dict = self.driver.get_window_size()
        print(window_size_dict)
        width_window = window_size_dict['width']
        height_window = window_size_dict['height']
        start_x = end_x = int(width_window) * 0.4
        start_y = int(height_window) * 0.8
        end_y = int(height_window) * 0.2
        time = 0
        ele = None
        while time < times:
            ele = self.find_element(locator, 2)
            if ele:
                break
            self.driver.swipe(start_x, start_y, end_x, end_y, 5000)
            time = time + 1
        if time > times:
            return None
        return ele

    def scroll_and_click(self, locator: tuple, times=1000):
        '''

        :param locator: 要查找的元素定位
        :param times: 滑动次数，默认直到找到为止
        :return: element
        '''
        ele = self.scroll_find_element(locator, 5)
        if ele:
            return ele.click()
        else:
            return None

    def find_and_input(self, locator, text, timeout=50):
        ele = self.find_element(locator, timeout)
        if ele:
            ele.send_keys(text)
        else:
            return None

    def find_token_exist(self, text=None):
        # print(self.driver.page_source)
        if text is not None:
            return self.find_element(text)
        else:
            return self.find_element((MobileBy.CLASS_NAME, "android.widget.Toast"))


